import { observable, computed, action, autorun, reaction, toJS } from "mobx";
import reqwest from 'reqwest';

import couponModal from "../modals/couponModal.js";
import goodsModal from "../modals/goodsModal.js";
import palaceModal from "../modals/palaceModal.js";
import messModal from "../modals/messModal.js";
import config from './config.js';

export default class MyStoreModel {
  @observable palaceList = [];
  @observable goodsList = [];
  @observable couponList = [];
  @observable messList = [];
  @observable loginCode = '';
  @observable liveEnter = null;
  @observable music = null;

  @action query(url, cb = () => {}) {
    reqwest({
      url: config.serverUrl + url,
      method: 'post',
      success: (data) => {
        cb(data)
      },
      error: (err) => {
        console.log('err: ', err);
        cb(false);
      }
    })
  }

  @action addModal(data) {}

  @action init() {
    this.palaceList = [];
    this.goodsList = [];
    this.couponList = [];
    this.messList = [];
    const urls = [
      'palace.do/getPalaceList',
      //'goods.do/getGoodsList',
      'coupon.do/getCouponList',
      'other.do/getOtherList',
      'message.do/getMessageList'
    ];
    let promises = urls.map((item, index) => {
      return new Promise(( resolve, reject ) => {
        this.query(item, (data) => {
          resolve(data)
        })
      });
    });
    let goods = Promise.all(promises).then(res => {
      console.log("========", res);
      res[0].map((item, index) => {
        this.palaceList.push(new palaceModal(item, this))
      });
      //res[1].map((item, index) => {
      //  this.goodsList.push(new goodsModal(item, this))
      //});
      res[1].map((item, index) => {
        this.couponList.push(new couponModal(item, this))
      });
      res[2].map((item, index) => {
        //console.log('res[3]=item==', item);
        if(item.o_name === 'o_liveEnter') {
          this.liveEnter = item.o_content;
        }
        if(item.o_name === 'o_music') {
          this.music = item.o_content;
        }
      });
      console.log('this.liveEnter..res[3]', res[3])
      console.log('this.liveEnter..this.music', this.liveEnter, this.music)
      res[3].map((item, index)=> {
        this.messList.push(new messModal(item.m_id, item.m_content, item.m_time))
      });
    });

  }

  @action getInGoods(p_id, getData = true) {
    const palaceList = toJS(this.palaceList);
    let palaceGoodsList, data = [];
    palaceList.map((item, index) => {
      if(item.data.p_id == p_id) {
        palaceGoodsList = item.data.p_goodsList;
      }
    });
    if(getData) {
      palaceGoodsList.map((item, index) => {
        item.key = index
      });
    }
    return palaceGoodsList;
  }

  @action getNotinGoods(p_id) {
    const goodsList = toJS(this.goodsList);
    let palaceGoodsList = this.getInGoods(p_id, false), data = [];

    const notInGoodsList = goodsList.filter((item) => {
      let notHas = true;
      for(let i = 0; i < palaceGoodsList.length; i++) {
        if (item.data.g_id === palaceGoodsList[i].g_id) notHas = false;
      }
      if(notHas) return item;
      return null;
    });
    notInGoodsList.map((item, index) => {
      item.data.key = index;
      data.push(item.data)
    });
    return data;
  }

  @action addCoupon(data) {
    console.log('data', data);
    reqwest({
      url: config.serverUrl +'coupon.do/addCoupon',
      method: 'post',
      data: { ...data }
    }).then((data) => {
      console.log('data: ', data);
      if(data.succ) {
        this.init()
      }
    });
  }

  @action editOther(o_id, o_content) {
    console.log(o_id, o_content);
    reqwest({
      url: config.serverUrl +'other.do/editOther',
      method: 'post',
      data: {
        o_id,
        o_content
      }
    }).then((data) => {
      console.log('data: ', data);
      if(data) {
        this.init()
      }
    });
  }

  @action delMess(m_id) {
    console.log('m_id', m_id);
    reqwest({
      url: config.serverUrl +'message.do/delMessage',
      method: 'post',
      data: { m_id }
    }).then((data) => {
      console.log('data: ', data);
      if(data.succ) {
        this.init()
      }
    });
  }

  @action addMess(m_content) {
    console.log('m_content', m_content);
    reqwest({
      url: config.serverUrl +'message.do/addMessage',
      method: 'post',
      data: { m_content }
    }).then((data) => {
      console.log('data: ', data);
      if(data.succ) {
        this.init()
      }
    });
  }

  @action setLogin(code) {
    console.log('code', code);
    console.log('code', config.serverUrl + 'goods.do/getSession');
    this.loginCode = code;
    reqwest({
      url: config.serverUrl + 'goods.do/getSession',
      method: 'post',
      data: {code}
    }).then((data) => {
      console.log('data: ', data);
      //if (data.succ) {
      this.init()
      //}
    });
  }

  @action addGoods(data) {
    console.log('data', data);
    reqwest({
      url: config.serverUrl + 'goods.do/addGoods',
      method: 'post',
      data: {...data}
    }).then((data) => {
      console.log('data: ', data);
      if (data.succ) {
        this.init()
      }
    });
  }

  @action addPalace() {
    if(!this.palaceList.length){
      reqwest({
        url:config.serverUrl +'palace.do/addPalace',
        method: 'post'
      }).then((data) => {
        console.log('data: ', data);
        if(data.succ) {
          this.init()
        }
      });
    }
  }
}
